package com.yikaiye.android.yikaiye.ui.adapter;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.yikaiye.android.yikaiye.R;
import com.yikaiye.android.yikaiye.application.MyApplication;
import com.yikaiye.android.yikaiye.config.Constant;
import com.yikaiye.android.yikaiye.databinding.ItemHomeTopTypeBinding;
import com.yikaiye.android.yikaiye.model.TypeProductModel;
import com.yikaiye.android.yikaiye.ui.activity.home.ProductListActivity;
import com.yikaiye.android.yikaiye.utils.JumpIntent;
import com.yikaiye.android.yikaiye.utils.ScreenUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 首页产品分类适配器
 */
@SuppressLint("NotifyDataSetChanged")
public class HomeTypeAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private List<TypeProductModel> data;
    private final Context context;

    public HomeTypeAdapter(Context context, List<TypeProductModel> data) {
        this.context = context;
        this.data = data;
    }
    public void onRefresh(List<TypeProductModel> mList) {
        if (mList != null) {
            if (data == null) {
                data = new ArrayList<>();
            }
            this.data.clear();
            this.data.addAll(mList);
            notifyDataSetChanged();
        }
    }
    public void addData(List<TypeProductModel> mList) {
        if (mList != null && mList.size() != 0) {
            if (data == null) {
                data = new ArrayList<>();
            }
            this.data.addAll(mList);
            notifyDataSetChanged();
        }
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ProductHolder(ItemHomeTopTypeBinding.inflate(LayoutInflater.from(context), parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        final ProductHolder productHolder = (ProductHolder) holder;
        TypeProductModel model = data.get(position);
        productHolder.binding.tvTypeName.setText(model.getName());
        Glide.with(context).load(model.getIcon()).placeholder(R.mipmap.icon_type).error(R.mipmap.icon_type).into(productHolder.binding.ivIcon);
        ViewGroup.LayoutParams layoutParams = productHolder.binding.llItem.getLayoutParams();
        layoutParams.width = ScreenUtil.getMaxWidth(context) / 4;
        productHolder.binding.llItem.setLayoutParams(layoutParams);
        productHolder.binding.llItem.setOnClickListener(view -> {
            Bundle bundle = new Bundle();
            bundle.putInt(Constant.INDEX_KEY, position);
            JumpIntent.jump((Activity) context, ProductListActivity.class, bundle);
        });
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    static class ProductHolder extends RecyclerView.ViewHolder {
        ItemHomeTopTypeBinding binding;

        public ProductHolder(ItemHomeTopTypeBinding itemHomeTopTypeBinding) {
            super(itemHomeTopTypeBinding.getRoot());
            binding = itemHomeTopTypeBinding;
        }
    }
}
